package com.exercises;

public class Exercise33 {

    public static void main(String[] args) {
        System.out.println(new Exercise33().search(new int[]{4, 5, 6, 7, 0, 1, 2}, 0));
    }

    public int search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while (l < r) {
            int m = (l + r) / 2;
            if ((nums[0] > target) ^ (nums[0] > nums[m]) ^ (target > nums[m])) {
                l = m + 1;
            } else {
                r = m;
            }
        }
        return l == r && nums[l] == target ? l : -1;
    }
}
